package com.ias.agdis_r.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import com.ias.agdis_r.R;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.util.date.PrettyDataFormaterUtil;

/**
 * 消息盒子Adapter
 * @author guoxr
 *
 */
public class BoxAdapter extends BaseSwipeAdapter {
    private List<Map<String, Object>> list = null;
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);

    // 上下文对象
    private Context mContext;

    // 构造函数
    public BoxAdapter(Context mContext, List<Map<String, Object>> list) {
        this.mContext = mContext;
        this.list = list;
    }

    // SwipeLayout的布局id
    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_boxlist,
                parent, false);
        final SwipeLayout swipeLayout = (SwipeLayout) v
                .findViewById(getSwipeLayoutResourceId(position));
        // 设置侧滑不可用
        swipeLayout.setSwipeEnabled(false);
        return v;
    }

    // 对控件的填值操作独立出来了，我们可以在这个方法里面进行item的数据赋值
    @Override
    public void fillValues(int position, View convertView) {
        TextView no = (TextView) convertView.findViewById(R.id.no);
        TextView msg = (TextView) convertView.findViewById(R.id.msg);
        TextView type = (TextView) convertView.findViewById(R.id.type);
        TextView time = (TextView) convertView.findViewById(R.id.time);
        no.setText(position + 1 + "");
        String str = (String) list.get(position).get("type");
        if (str != null) {
            if (str.equals(MessageModel.DYHB)) {
                type.setText(mContext.getString(R.string.flightChange));
                msg.setText((String) list.get(position).get("msg"));
            } else if (str.equals(MessageModel.BLRQ)) {
                type.setText(mContext.getString(R.string.docInfo));
                msg.setText((String) list.get(position).get("msg"));
            } else if (str.equals(MessageModel.TRST)) {
                type.setText(mContext.getString(R.string.stateChange));
                msg.setText((String) list.get(position).get("msg"));
            } else if (str.equals(MessageModel.TTUD)) {
                type.setText(mContext.getString(R.string.infoChange));
                msg.setText((String) list.get(position).get("msg"));
            } else if (str.equals(MessageModel.TTSD)) {
                type.setText(mContext.getString(R.string.taskReceive));
                msg.setText((String) list.get(position).get("msg"));
            } else if (str.equals(MessageModel.TTAL)) {
                type.setText(mContext.getString(R.string.transferInfo));
                msg.setText((String) list.get(position).get("msg"));
            } else if (str.equals(MessageModel.TTAS)) {
                type.setText(mContext.getString(R.string.sendtoInfo));
                msg.setText((String) list.get(position).get("msg"));
            }
            try {
                time.setText(PrettyDataFormaterUtil.formateDate(DATE_FORMAT.parse((String) list.get(position).get("time")), mContext));
            }catch (ParseException e) {
                time.setText((String) list.get(position).get("time"));
            }
        } else {
            type.setText(mContext.getString(R.string.messageInfo));
            msg.setText(mContext.getString(R.string.leftBracket) + list.get(position).get("sem")
                    + mContext.getString(R.string.BA_1) + list.get(position).get("mtn") + mContext.getString(R.string.BA_2)
                    + list.get(position).get("txt"));
            try {
                time.setText(PrettyDataFormaterUtil.formateDate(DATE_FORMAT.parse((String) list.get(position).get("stm")), mContext));
            }catch (ParseException e) {
                time.setText((String) list.get(position).get("stm"));
            }
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
